package com.bridgeintelligent.tag.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by JackCheung on 2019/9/11 3:57 PM.
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class ApiDocs implements WebMvcConfigurer {

    /**
     * 标签管理配置
     *
     * @return
     */
    @Bean
    public Docket dimMgmtConfig() {
        return DimMgmtApis.docs();
    }

    /**
     * 用户管理配置
     *
     * @return
     */
    @Bean
    public Docket userMgmtConfig() {
        return UserMgmtApis.docs();
    }

    /**
     * 安全配置
     *
     * @return
     */
    @Bean
    public Docket SecurityConfig() {
        return SecurityApis.docs();
    }

    /**
     * 搜索引擎配置
     *
     * @return
     */
    @Bean
    public Docket engineConfig() {
        return EngineApis.docs();
    }

    /**
     * 客群配置
     *
     * @return
     */
    @Bean
    public Docket CustomersConfig() {
        return CustomersApis.docs();
    }

    /**
     * 查询配置
     *
     * @return
     */
    @Bean
    public Docket queryConfig() {
        return QueryApis.docs();
    }

    /**
     * 组合标签配置
     *
     * @return
     */
    @Bean
    public Docket CombinationConfig() {
        return CombinationApis.docs();
    }


    /**
     * 组合标签配置
     *
     * @return
     */
    @Bean
    public Docket logConfig() {
        return LogApis.docs();
    }


    /**
     * 场景
     *
     * @return
     */
    @Bean
    public Docket sceneConfig() {
        return SceneApis.docs();
    }
   /**
     * 系统配置模块
     *
     * @return
     */
    @Bean
    public Docket SystemConfig() {
        return SystemApis.docs();
    }

    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
            WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier,
            ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes,
            CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = webEndpointProperties.getDiscovery().isEnabled() &&
                                             (org.springframework.util.StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }
}